/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package plash.display;

import plash.events.Event;
import plash.events.EventDispatcher;
import plash.events.IEventDispatcher;
import plash.events.IEventListener;
import plash.events.MouseEvent;
import plash.geom.Rectangle;
import plash.utils.ColorMapper;
import processing.core.PApplet;
import processing.core.PFont;
import processing.core.PGraphics;

/**
 *
 * @author #0K Srinivasan <hashok.com>
 */
public class Stage extends PApplet implements IStage, IEventDispatcher
{
    protected int __backgroundColor = 0xFFFFFF;
    protected ColorMapper __color;
    protected Rectangle __size;
    protected EventDispatcher _dispatcher = new EventDispatcher();
    private DisplayObjectContainer __root;
    private PGraphics __stagePainter;
    private PGraphics __maskingPainter;
    private PGraphics __maskablePainter;
    private PGraphics __tempPainter;
    
    public Stage()
    {
        this(new Rectangle(0, 0, 500, 500), 0xFFFFFF);
    }

    public Stage(Rectangle stageSize, int backgroundColor)
    {
        _initialize(stageSize, backgroundColor);
    }

    private void _initialize(Rectangle stageSize, int backgroundColor)
    {
        __color = new ColorMapper(backgroundColor);
        __size = stageSize;
        __backgroundColor = __color.getRGB();

        __root = new Sprite();
        __root.setIsRoot(true);
        __root.setName("_root");
    }

    @Override
    public void setup()
    {
        size(__size.getWidth(), __size.getHeight(), JAVA2D);
        background(__color.getRed(), __color.getGreen(), __color.getBlue(), __color.getAlpha());

        __stagePainter = this.createGraphics(__size.getWidth(), __size.getHeight(),JAVA2D);
        __maskingPainter = this.createGraphics(__size.getWidth(), __size.getHeight(),JAVA2D);
        __maskablePainter = this.createGraphics(__size.getWidth(), __size.getHeight(),JAVA2D);
        __tempPainter = this.createGraphics(__size.getWidth(), __size.getHeight(),JAVA2D);
                
        __root.setStage(this);
        __root.setPainter(__stagePainter);
    }

    @Override
    public void draw()
    {
        if (__root == null)
        {
            return;
        }
        if (__stagePainter != null)
        {
            __stagePainter.beginDraw();
            __stagePainter.background(__color.getRed(), __color.getGreen(), __color.getBlue(), __color.getAlpha());
            
//            __maskablePainter.beginDraw();            
//            __maskingPainter.beginDraw();
            
            __root.draw();
            
//            __maskingPainter.endDraw();
//            __maskablePainter.endDraw();
            
            __stagePainter.endDraw();

            this.image(__stagePainter, 0, 0);
        }
    }

    @Override
    public void mousePressed()
    {
        dispatchEvent(new MouseEvent(MouseEvent.MOUSE_DOWN, mouseX, mouseY));
    }

    @Override
    public void mouseReleased()
    {
        dispatchEvent(new MouseEvent(MouseEvent.MOUSE_UP, mouseX, mouseY));
    }

    @Override
    public void mouseDragged()
    {
        dispatchEvent(new MouseEvent(MouseEvent.MOUSE_MOVE, mouseX, mouseY));
    }

    @Override
    public void mouseMoved()
    {
        dispatchEvent(new MouseEvent(MouseEvent.MOUSE_MOVE, mouseX, mouseY));
    }

    public DisplayObject addChild(DisplayObject child)
    {
        return __root.addChild(child);
    }

    public DisplayObject removeChild(DisplayObject child)
    {
        return __root.removeChild(child);
    }

    public DisplayObject getChildByName(String searchName)
    {
        return __root.getChildByName(searchName);
    }

    public DisplayObject getChildByIndex(int index) throws IndexOutOfBoundsException
    {
        return __root.getChildByIndex(index);
    }

    public boolean contains(DisplayObject toSearch)
    {
        return __root.contains(toSearch);
    }

    @Override
    public int getWidth()
    {
        return __size.getWidth();
    }

    @Override
    public int getHeight()
    {
        return __size.getHeight();
    }

    public int getBackgroundColor()
    {
        return __backgroundColor;
    }

    public Rectangle getStageSize()
    {
        return __size;
    }

    @Override
    public PGraphics getStagePainter()
    {
        return __stagePainter;
    }

    @Override
    public PGraphics getMaskablePainter()
    {
        return __maskablePainter;
    }

    @Override
    public PGraphics getMaskingPainter()
    {
        return __maskingPainter;
    }

    @Override
    public PGraphics getTempPainter()
    {
        return __tempPainter;
    }

    @Override
    public void addEventListener(String name, IEventListener listener)
    {
        _dispatcher.addEventListener(name, listener);
    }

    @Override
    public void removeEventListener(String name, IEventListener listener)
    {
        _dispatcher.removeEventListener(name, listener);
    }

    @Override
    public void dispatchEvent(Event e)
    {
        _dispatcher.dispatchEvent(e);
    }

    @Override
    public int getMouseX()
    {
        return this.mouseX;
    }

    @Override
    public int getMouseY()
    {
        return this.mouseY;
    }

    @Override
    public PFont createFont(String name, int size, boolean smooth)
    {
        return super.createFont(name, size, smooth);
    }
}